<template>
  <div class="panel">
    <!-- 完成新增修改 -->
    <el-dialog title="编辑权限" :visible.sync="showDialog" @close="cancle">
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="企业可见">
          <el-switch
            v-model="form.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPerDataApi, uploadPerApi, addPerApi } from "@/api/permission.js";
export default {
  name: "panel",
  props: ["showDialog"],
  data() {
    return {
      // 数据源
      form: {
        name: "",
        code: "",
        description: "",
        enVisible: ""
      }
    };
  },
  methods: {
    // 得到 type & pid 的值
    getData(type, pid) {
      this.form.type = type;
      this.form.pid = pid;
      // console.log(this.form);
    },
    // 取消关闭面板
    cancle() {
      this.$emit("update:showDialog", false);
      // 关闭面板 清空面板
      this.form = {
        name: "",
        code: "",
        description: "",
        enVisible: ""
      };
    },
    // 点击提交吧数据上传服务器并关闭面板
    async submit() {
      if (this.form.id) {
        // 提示修改数据
        await uploadPerApi(this.form);
      } else {
        await addPerApi(this.form);
      }
      // 提示一下
      this.$message.success(this.form.id ? "修改成功" : "新增成功");
      // 关闭面板
      this.$emit("update:showDialog", false);
      // 关闭面板 清空面板
      this.form = {
        name: "",
        code: "",
        description: "",
        enVisible: ""
      };
      // 更新数据
      this.$emit("upDateData");
    },
    // 根据 id 获取详情
    async getPerData(id) {
      const res = await getPerDataApi(id);
      this.form = res.data;
    }
  },
  created() {
    this.getPerData;
    this.addPerApi;
  }
};
</script>

<style></style>
